package com.situ.hospital.department.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.hospital.department.dao.IDepartmentDao;
import com.situ.hospital.department.model.DepartmentModel;

import util.JDBCUtil;

public class DepartmentDaoImpl implements IDepartmentDao {
	@Override
	public Integer insert(DepartmentModel department) {
		String sql = "insert into department(department_name,department_address,department_tel) values(?,?,?)";
		return JDBCUtil.dml(sql, department.getDepartmentName(), department.getDepartmentAddress(),
				department.getDepartmentTel());
	}

	@Override
	public DepartmentModel selectModel(DepartmentModel department) throws SQLException {
		String sql = "select * from department where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, department, 2);
		List<DepartmentModel> backlist = JDBCUtil.dql(sql, list, DepartmentModel.class, department.mapGet());
		if (backlist == null || backlist.size() == 0) {
			return null;
		} else {
			return backlist.get(0);
		}
	}

	@Override
	public List<DepartmentModel> selectAll(DepartmentModel department) throws SQLException {
		String sql = "select department.*,sum from department left join(select doctor.department_name,sum(1) sum from doctor group by department_name) doc on department.department_name=doc.department_name where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, department, 2);
//		System.out.println(sql);
		return JDBCUtil.dql(sql, list, DepartmentModel.class, department.mapGet());
	}

	@Override
	public Integer deleteByName(DepartmentModel department) throws SQLException {
		String sql = "delete from department where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, department, 2);
		return JDBCUtil.dml(sql, list);
	}

	@Override
	public Integer updateByName(DepartmentModel department) throws SQLException {
		String sql = "update department set ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, department, 3);
		return JDBCUtil.dml(sql, list);
	}

	@Override
	public Integer selectCount(DepartmentModel dm) throws SQLException {
		String sql = "select count(1) from department where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, dm, 2);
		return JDBCUtil.selectCount(sql, list);

	}

	/**
	 * @param sql
	 * @param list
	 * @param department
	 * @param n          1为insert 2为select 2为delete 3为update
	 * @return
	 */
	private String setSql(String sql, List<Object> list, DepartmentModel department, int m) {
		int n = 0;
		if (department.getId() != null && department.getId() == 1) {
			sql += "department.department_name like ? and department.department_address like ?";
			list.add(department.getDepartmentName());
			list.add(department.getDepartmentAddress());
			if (department.isPageOn()) {
				sql += " limit ?,?";
				list.add(department.getRowStart());
				list.add(department.getPageLimit());
			}
			return sql;
		}
		String name = department.getDepartmentName();
		if (name != null && !name.equals("") && m != 3) {
			if (n == 1) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "department_name=?";
			list.add(department.getDepartmentName());
			n = m;
		}
		String address = department.getDepartmentAddress();
		if (address != null && !address.equals("")) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "department_address=?";
			list.add(department.getDepartmentAddress());
			n = m;
		}
		String tel = department.getDepartmentTel();
		if (tel != null && !tel.equals("")) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "department_tel=?";
			list.add(department.getDepartmentTel());
			n = m;
		}
		if (m == 3) {
			sql += "where department_name=?";
			list.add(department.getDepartmentName());
		}

		return sql;
	}

}
